# coding=utf-8
# __author__ = 'chenbinghui'
#!/usr/bin/python
# -*- coding: UTF-8 -*-

import urllib2
import urllib
import re
import thread
import time

#糗事百科爬虫类
class Qsbk_Crawler:
    #初始化方法，定义一些变量
    def __init__(self):
        self.pageIndex = 1
        self.user_agent = 'Mozilla/4.0 (compatible; MSIE 5.5; Windows NT)'
        self.headers = {'User-Agent':self.user_agent}
        self.enable = False
        #存放段子的变量，每一个元素是每一页的段子们
        self.stories = []
        self.sorted_stories =[]
    #传入某一页的索引获取页面代码
    def getPage(self,pageIndex):
        try:
            url = 'http://www.qiushibaike.com/text/page/' + str(pageIndex)
            #构建请求的request
            request = urllib2.Request(url,headers=self.headers)
            #利用urlopen获取页面代码
            response = urllib2.urlopen(request)
            pageCode = response.read().decode('utf-8')
            return pageCode
        except urllib2.URLError,e:
            if hasattr(e,'reason'):
                print u'连接糗事百科失败，错误原因',e.reason
                return None
    #传入某一页代码，返回本页不带图片的段子列表
    def getPageItems(self,pageIndex):
        pageCode = self.getPage(pageIndex)

        if not pageCode:
            print '页面加载失败...'
            return None
        pattern = re.compile('<div.*?author clearfix">.*?<a.*?<h2>(.*?)</h2>.*?<a.*?<div.*?' +
                             '<span>(.*?)</span>.*?<div class="stats.*?number">(.*?)</i>', re.S)
        items = re.findall(pattern, pageCode)
        pageStories=[]
        #遍历正则表达式匹配的信息
        for item in items:
            replaceBR = re.compile('<br/>')
            text = re.sub(replaceBR,'\n',item[1])
            pageStories.append((item[0].strip(),text.strip(),int(item[2].strip())))
        return pageStories
    #加载并提取页面的内容，加入到列表中
    def loadPage(self):
        if self.enable == True:
            if len(self.stories) < 2:
                #获取新页面
                pageStories = self.getPageItems(self.pageIndex)
                if pageStories:
                    self.stories.append(pageStories)
                    self.pageIndex +=1

    #调用该方法，每次敲回车打印输出一个段子
    def getOneStory(self,pageStories,page):
        #遍历一页的段子
        for stroy in pageStories:
            #等待用户输入
            input = raw_input()
            self.loadPage()
            if input == 'Q' or input == 'q':
                self.enable = False
                return
            print u"第%d页\t发布人:%s\t点赞数:%s\n%s" %(page,stroy[0],stroy[2],stroy[1])
     #开始方法
    def start(self):
        print u'正在读取糗事百科，按回车查看新段子，Q或者q退出'
        self.enable = True
        #先加载一页
        self.loadPage()
        #局部变量，控制当前读到第几页
        nowPage = 0
        while self.enable:
            if len(self.stories)>0:
                #从全局list中获取一页的段子
                pageStories = self.stories[0]
                #当前读到的页码加1
                nowPage +=1
                del self.stories[0]
                self.getOneStory(sorted(pageStories, key=lambda s: s[2], reverse=True),nowPage)


spider = Qsbk_Crawler()
spider.start()


